package com.example.shiwuyouproject.ui.fragment.my.activity

import android.content.Intent
import com.example.shiwuyouproject.base.BaseVmActivity
import com.example.shiwuyouproject.databinding.ActivityTeachercertificationBinding
import com.example.shiwuyouproject.net.http.QiNiuUpImageUtils
import com.example.shiwuyouproject.ui.fragment.my.activity.viewmodel.TeacherCertificationViewModel
import com.example.shiwuyouproject.ui.my.bean.PersonalInformationBean
import com.example.shiwuyouproject.utils.GlideUtils
import com.luck.picture.lib.config.PictureConfig
import com.yechaoa.yutilskt.ToastUtil
import com.yechaoa.yutilskt.YUtils

/**
 * 教师资格证
 */
class TeacherCertificationActivity :BaseVmActivity<ActivityTeachercertificationBinding, TeacherCertificationViewModel>(){
    override fun viewModelClass(): Class<TeacherCertificationViewModel> = TeacherCertificationViewModel::class.java

    override fun getViewBinding(): ActivityTeachercertificationBinding {
        return ActivityTeachercertificationBinding.inflate(layoutInflater)
    }
    private var teachno:String=""
    override fun initView() {
    mBinding.myTitleBar.tvTitle.text="教师资格"
        mViewModel.getRealNameAuthenticationData()

    }

    override fun initData() {

    }

    override fun setListener() {
    mBinding.myTitleBar.tvBack.setOnClickListener { finish() }
    mBinding.clUploadTheFrontOfIDCard.setOnClickListener { //上传教师资格证正面
        QiNiuUpImageUtils.openCard(this)
    }
        mBinding.btSava.setOnClickListener { //保存
            teachno=mBinding.eduno.text.toString()
            if(teachno.isEmpty()){
                ToastUtil.show("请输入整数编号！")
                return@setOnClickListener
            }
            YUtils.showLoading(this, "保存中...")
                    mViewModel.jiaoxuetedian(teachno,teachimg)
        }

    }
    private var teachimg=""
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK&&requestCode== PictureConfig.REQUEST_CAMERA) {
            QiNiuUpImageUtils.postUpQiNiuFile(data,object : QiNiuUpImageUtils.onBackImageStatus {
                override fun onSuccess(key: String) {
                    teachimg =key
                    GlideUtils.fangImgPortrait(this@TeacherCertificationActivity,mBinding.ivEduimg,teachimg)
                }
                override fun onFail() {
                    YUtils.hideLoading()
                    ToastUtil.show("图片上传失败，请检查网络连接")
                }
            })
        }
    }
    override fun observe() {
    mViewModel.getGenRenStatus.observe(this,androidx.lifecycle.Observer<PersonalInformationBean>{
        teachimg=it.teachimg
        teachno=it.teachno
        GlideUtils.fangImgPortrait(this,mBinding.ivEduimg,teachimg)
        mBinding.eduno.setText(teachno)
    })
        mViewModel.getPostStatus.observe(this,androidx.lifecycle.Observer<Boolean>{
            if(it){
                YUtils.hideLoading()
                ToastUtil.show("提交成功！")
                finish()
            }
        })
    }
}